const app = getApp()
const $api = require('../../utils/api.js').API;

Page({
    data: {
        imgUrl:app.globalData.imgUrl,
        banner:'',
        school:[],
        page:1,
        totalPge:0,
        city:'全部',
        keyword:''
    },
    onLoad: function(){
        wx.showLoading()
        this.getBanner();
        this.getSchool();
    },
    getBanner:function(){
      var that = this;
      var data = {'type':7}
      $api.get('banner',data).then(res => {
          //请求成功
          if(res.data.code == 1){
            that.setData({
              banner:res.data.data[0].img
            })
          }
      })
    },
    getSchool:function(){
        var that = this
        if(this.data.city == '全部'){
          var city = '';
        }else{
          var city = this.data.city;
        }
        var data = {
          page:this.data.page,
          city:city,
          keyword:this.data.keyword
        }
        var school = this.data.school
        $api.get('company/is_lease',data).then(res => {
            //请求成功
            if(res.data.code == 1){
              if(that.data.totalPage == 0){
                that.setData({
                  totalPage:res.data.data.last_page
                })
              }
              wx.hideLoading()
              if(that.data.page == 1){
                that.setData({
                  school:res.data.data.data
                })
              }else{
                  that.setData({
                    school:school.concat(res.data.data.data)
                  })
              }
            }
        })
    },
    info:function(e){
        var id = e.currentTarget.dataset.id
        wx.navigateTo({
            url:'/pages/lease/info?id='+id
        })
    },
    onReachBottom: function(){
      if(this.data.page < this.data.totalPage){
          //当前不是最后一页可加载
          this.setData({
              page: this.data.page + 1,
          })
          wx.showLoading()
          this.getSchool();
      }
  },
  jump:function(e){
    var url = e.currentTarget.dataset.key
    wx.navigateTo({
      url:url
    })
  },
  search:function(e){
    var value = e.detail.value
    this.setData({
      keyword:value
    })
    wx.showLoading()
    this.getSchool();
  },
  onShareAppMessage: function() {
      return {
          title: "天意美业直通车",
          path: "/pages/index/index?puid="+wx.getStorageSync('uid')
      };
  }
});